package hongcaosp.app.android.video;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;

import com.blackchopper.loginshare.impl.LoginShare;
import com.blackchopper.loginshare.interfaces.OnLoginshareListener;
import com.blackchopper.loginshare.model.Type;
import com.blackchopper.loginshare.model.WechatMessageBody;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import hongcaosp.app.android.R;
import hongcaosp.app.android.comment.CommentDialog;
import hongcaosp.app.android.common.PresenterListener;
import hongcaosp.app.android.contact.IMActivity;
import hongcaosp.app.android.login.UserToken;
import hongcaosp.app.android.modle.bean.IVideo;
import hongcaosp.app.android.video.player.VideoPresenter;
import xlj.lib.android.base.toastcompat.ToastManager;
import xlj.lib.android.base.view.BottomDialog;
import xlj.lib.android.base.view.Item;
import xlj.lib.android.base.view.OnItemClickListener;

public class ShareUI implements OnLoginshareListener {
    private List<Item> items;
    private Context context;
    private IVideo video;
    private BottomDialog bottomDialog;

    private ShareCallBack shareCallBack;

    private LoginShare loginShare;

    public void setShareCallBack(ShareCallBack shareCallBack) {
        this.shareCallBack = shareCallBack;
    }

    public ShareUI(Context context, IVideo video) {
        this.context = context;
        this.video = video;
        items = new ArrayList<>();

        Item item = new Item();
        item.setTitle("私信");
        item.setIcon(context.getResources().getDrawable(R.drawable.icon_share_message));
        item.setId(1);
        items.add(item);

        Item resend = new Item();
        resend.setTitle("转发");
        resend.setIcon(context.getResources().getDrawable(R.drawable.icon_share_reforword));
        resend.setId(3);
        items.add(resend);

        Item shareItem = new Item();
        shareItem.setTitle("分享到");
        shareItem.setIcon(context.getResources().getDrawable(R.drawable.icon_share_wechat));
        shareItem.setId(2);
        items.add(shareItem);

//        Item favorite = new Item();
//        favorite.setIcon(context.getResources().getDrawable(R.drawable.icon_share_favorite));
//
//        if (video.isCollection()) {
//            favorite.setId(3);
//            favorite.setTitle("取消收藏");
//        } else {
//            favorite.setId(4);
//            favorite.setTitle("收藏");
//        }
//        items.add(favorite);

        Item save = new Item();
        save.setTitle("保存");
        save.setId(5);
        save.setIcon(context.getResources().getDrawable(R.drawable.icon_share_save));
        items.add(save);


    }

    public void show() {
        loginShare = new LoginShare((AppCompatActivity) context);
        loginShare.register(this);
        if (bottomDialog == null) {
            bottomDialog = new BottomDialog(context);
            bottomDialog.layout(BottomDialog.GRID).orientation(BottomDialog.VERTICAL).addItems(items,
                    new OnItemClickListener() {
                        @Override
                        public void click(Item item) {
                            dismiss();
                            if (item.getId() == 5) {
                                //保存
                                save();
                            } else if (item.getId() == 1) {
                                message();
                            } else if (item.getId() == 2) {
                                share();
                            } else if (item.getId() == 3) {
                                reForword();
                            }
                        }
                    }).background(R.drawable.shape_corner_up_blue);
        }
        bottomDialog.show();
    }



    /**
     * 分享
     */
    public void share() {
        if (shareCallBack != null) {
            shareCallBack.onShare(video);
        }

        WechatMessageBody wechatMessageBody = new WechatMessageBody();
        wechatMessageBody.msgType = WechatMessageBody.MSG_VIDEO;
        wechatMessageBody.type(WechatMessageBody.WECHAT);
        wechatMessageBody.imageUrl(video.getVideoImage());
        wechatMessageBody.videoUrl(video.getVideoUrl());
        wechatMessageBody.title("红草视频");
        wechatMessageBody.description(video.getDescrip());
        loginShare.launchWechatShare(wechatMessageBody);
    }

    private void reForword() {
        if (UserToken.getDefault().isLogged(context)) {
            CommentDialog commentDialog = CommentDialog.create(context, new CommentDialog.CommentCallBack() {
                @Override
                public void onComment(String content) {
                    VideoPresenter videoPresenter = new VideoPresenter();
                    videoPresenter.forwordVideo(content, video, new PresenterListener() {
                        @Override
                        public void onSuccess() {
                            ToastManager.getInstance().showToast("转发成功");
                        }

                        @Override
                        public void onFail(String message) {
                            ToastManager.getInstance().showToast(message);
                        }
                    });
                }
            });
            commentDialog.show();
        }
    }

    /**
     * 私信
     */
    private void message() {
        if (UserToken.getDefault().isLogged(context)) {
            IMActivity.toIM(context,video.getAuthor().getUserName(),UserToken.getDefault().getUser().getUserId() + "",
                    video.getAuthor().getUserId() + "");
        }
    }

    private void save() {
        VideoDownload videoDownload = new VideoDownload(context,video);
        videoDownload.start();
    }

    public void dismiss() {

        if (bottomDialog != null) {
            bottomDialog.dismiss();
            bottomDialog = null;
        }
    }

    @Override
    public void onLoginSuccess(Type type, String code, String info) {

    }

    @Override
    public void onLoginCancel(Type type) {

    }

    @Override
    public void onLoginError(Type type, int errorCode) {

    }

    @Override
    public void onShareSuccess(Type type) {
//        ToastManager.getInstance().showToast("发送成功");
        loginShare.unRegister();
    }

    @Override
    public void onShareCancel(Type type) {
        ToastManager.getInstance().showToast("分享取消");
        loginShare.unRegister();
    }

    @Override
    public void onShareError(Type type, int code) {
        ToastManager.getInstance().showToast("分享失败");
        loginShare.unRegister();
    }

    public interface ShareCallBack {
        void onShare(IVideo video);
    }
}
